package com.digoyo.hr.service;

import com.digoyo.hr.mapper.HrMapper;
import com.digoyo.hr.model.Hr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author: Daniel
 * @Description:
 * @Date: Created in 5:41 PM 2020/3/9
 * @Modified By:
 */

@Service
public class HrService implements UserDetailsService{
    @Autowired
    HrMapper hrMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUsername(username);
        if (hr==null) {
            throw new UsernameNotFoundException("用户名不存在!");
        }
        hr.setRoles(hrMapper.getHrRolesById(hr.getId()));
        return hr;
    }

    public Hr getCurrentHr() {
        return (Hr)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
